import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
import json

class Teststatefulset:

    @pytest.mark.parametrize('statefulsetinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/statefulset/1_get_statefulset_list.yaml'))
    def test_get_statefulset_list(self, statefulsetinfo, get_env_info):
        # 获取statefulset列表接口
        baseUrl = get_env_info
        url = baseUrl + statefulsetinfo['request']['url'].replace('{namespace}','default')
        method = statefulsetinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == statefulsetinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('statefulsetinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/statefulset/2_create_statefulset.yaml'))
    def test_create_statefulset(self, statefulsetinfo, get_env_info):
        # 创建statefulset接口
        baseUrl = get_env_info
        url = baseUrl + statefulsetinfo['request']['url'].replace('{namespace}','default')
        method = statefulsetinfo['request']['method']
        data = statefulsetinfo['request']['data']
        req = RequestUtil().send_request(method, url,json=data)
        if '-' not in statefulsetinfo['name']:
            statefulsetname=req.json()['metadata']['name']
            globals()['statefulsetname']=statefulsetname
        assert req.status_code == statefulsetinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('statefulsetinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/statefulset/3_get_specific_statefulset.yaml'))
    def test_get_specific_statefulset(self, statefulsetinfo, get_env_info):
        # 获取指定statefulset接口
        baseUrl = get_env_info
        url = baseUrl + statefulsetinfo['request']['url'].replace('{namespace}','default').replace('{statefulsetname}',globals()['statefulsetname'])
        method = statefulsetinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == statefulsetinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('statefulsetinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/statefulset/4_update_statefulset.yaml'))
    def test_update_statefulset(self, statefulsetinfo, get_env_info):
        # 更新指定statefulset接口
        baseUrl = get_env_info
        url = baseUrl + statefulsetinfo['request']['url'].replace('{namespace}','default').replace('{statefulsetname}',globals()['statefulsetname'])
        method = statefulsetinfo['request']['method']
        data = statefulsetinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == statefulsetinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('statefulsetinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/statefulset/5_update_section_statefulset.yaml'))
    def test_update_section_statefulset(self, statefulsetinfo, get_env_info):
        # 部分更新指定statefulset的接口
        baseUrl = get_env_info
        url = baseUrl + statefulsetinfo['request']['url'].replace('{namespace}','default').replace('{statefulsetname}',globals()['statefulsetname'])
        method = statefulsetinfo['request']['method']
        data = statefulsetinfo['request']['data']
        header = statefulsetinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header,json=data)
        assert req.status_code == statefulsetinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('statefulsetinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/statefulset/6_delete_specific_statefulset.yaml'))
    def test_delete_specific_statefulset(self, statefulsetinfo, get_env_info):
        # 删除statefulset接口
        baseUrl = get_env_info
        url = baseUrl + statefulsetinfo['request']['url'].replace('{namespace}','default').replace('{statefulsetname}',globals()['statefulsetname'])
        method = statefulsetinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == statefulsetinfo['validate'][0]['equals']['status_code']